package com.museum.security;

import com.museum.entity.AdminUser;
import com.museum.service.IAdminUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * @author Akihi
 * @create 2022/11/13
 */
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    IAdminUserService adminUserService;
    //重写用户加载
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AdminUser adminUser = adminUserService.getByUsername(username);
        if (adminUser == null) {
            throw new UsernameNotFoundException("用户名或密码不正确!");
        }
        return new AccountUser(adminUser.getId(), adminUser.getUsername(), adminUser.getPassword(), Collections.emptyList());
    }
    /**
     * 获取用户权限信息（角色、菜单权限）
     * @param userId
     * @return
     */
    /*public List<GrantedAuthority> getUserAuthority(Long userId){
        // 通过内置的工具类，把权限字符串封装成GrantedAuthority列表
        // 角色(ROLE_admin)、菜单操作权限 sys:user:list
        String authority = sysUserService.getUserAuthorityInfo(userId);  // ROLE_admin,ROLE_normal,sys:user:list,....
        //使用 逗号分隔字符串到权限列表 方法进行转换
        return AuthorityUtils.commaSeparatedStringToAuthorityList(authority);
    }*/
}
